<?php

namespace App\Http\Controllers\H5;
use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\CustomerAuth;
use App\Params\H5\OrderCommentParams;
use App\Params\H5\OrderCreateParams;
use App\Params\H5\OrderListParams;
use App\Params\H5\OrderPayParams;
use App\Services\Sales\OrderService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;

/**
 * 客户订单相关控制器
 *
 * @author WangBin
 * @date 2021-09-20
 * @package App\Http\Controllers\H5
 */
class OrderController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        // 校验token中间件
        $this->middleware(CustomerAuth::class);
    }

    /**
     * 创建订单
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function create()
    {
        $param = OrderCreateParams::create();
        $result = OrderService::getInstance()->create($param);
        if(empty($result['order_id'])) {
            return $this->fail(ResponseCode::ORDER_CREATE_FAIL);
        }
        return $this->success($result);
    }

    /**
     * 订单支付
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws BusinessException
     */
    public function pay()
    {
        $param = OrderPayParams::create();
        $ret = OrderService::getInstance()->pay($param);
        if($ret) {
            return $this->success(['pay_type' => $param->getPayType()]);
        }
        return $this->fail(ResponseCode::PAY_FAIL);
    }

    /**
     * 客户订单统计信息
     *
     * @throws
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStat()
    {
        $customerId = $this->verifyInteger('customer_id');
        $ret = OrderService::getInstance()->getCustomerStat($customerId);
        return $this->success($ret);
    }

    /**
     * 客户订单列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $param = OrderListParams::create();
        $ret = OrderService::getInstance()->getCustomerOrderList($param);
        return $this->success($ret);
    }

    /**
     * 取消订单
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function cancel()
    {
        $orderId = $this->verifyInteger('order_id');
        $ret = OrderService::getInstance()->cancel($orderId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::ORDER_CANCEL_FAIL);
    }

    /**
     * 删除订单
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function delete(Request $request)
    {
        $orderId = $request->input('id', 0);
        $ret = OrderService::getInstance()->delete($orderId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::ORDER_DELETE_FAIL);
    }

    /**
     * 获取订单详情
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws BusinessException
     */
    public function get()
    {
        $orderId = $this->verifyInteger('order_id');
        $ret = OrderService::getInstance()->getH5Order($orderId);
        return $this->success($ret);
    }

    /**
     * 订单确认收货
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws BusinessException
     */
    public function receipt()
    {
        $orderId = $this->verifyInteger('order_id');
        OrderService::getInstance()->receipt($orderId);
        return $this->success($orderId);
    }

    /**
     * 订单评论
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function comment()
    {
        $param = OrderCommentParams::create();
        OrderService::getInstance()->comment($param);
        return $this->success();
    }
}
